import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';
import { LoggerInterceptor } from './interceptors/logger.interceptor';
import { TransformInterceptor } from './interceptors/transform.interceptor';
import { ExceptionInterceptor } from './interceptors/exception.interceptor';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  /** 应用Swagger */
  const config = new DocumentBuilder()
    .setTitle('Orange personal web server API')
    .setDescription('The orange-personal-web-server API description')
    .setVersion('1.0.0')
    // .setBasePath('/api') // 已经不生效了 用addServer代替
    .addServer('/api')
    .addTag('Orange personal web server')
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('docs', app, document);

  /** 开启跨域 */
  app.enableCors();

  /** 绑定自动验证 */
  app.useGlobalPipes(new ValidationPipe());

  /** 设置全局路由前缀 */
  app.setGlobalPrefix('api');

  /** 设置全局拦截器 */
  app.useGlobalInterceptors(new LoggerInterceptor());
  app.useGlobalInterceptors(new TransformInterceptor());
  app.useGlobalInterceptors(new ExceptionInterceptor());

  await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
